﻿/****************************** Module Header ******************************\
 * Module Name:  mxUserControl.cs
 * Module Version:  1.0.0 (2013-7-31)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension Form instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace UCL.Forms
{
    public static class mxUserControl
    {
        /// <summary>
        /// 打开子窗体对话框
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="uc"></param>
        public static void OpenDialog<T>(this UserControl uc, Func<T> act = null) where T : Form, new()
        {
            T t = act == null ? new T() : act();
            t.Tag = uc;
            t.StartPosition = FormStartPosition.CenterScreen;
            t.ShowInTaskbar = true;
            t.Show();
        }

        /// <summary>
        /// 刷新父用户控件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="f"></param>
        /// <param name="methodname"></param>
        public static void RefreshOwnerUserControl<T>(this Form f, string methodname = "FormLoad", params object[] args) where T : UserControl
        {
            if (f.Tag == null || f.Tag.GetType() != typeof(T)) return;
            T owner = (T)f.Tag;
            owner.GetType().InvokeMember(methodname, BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding, null, owner, args);
        }
    }
}
